package com.qf.openapi.web.master.service.impl;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import com.github.pagehelper.PageHelper;
import com.qf.openapi.web.master.dao.RoutDao;
import com.qf.openapi.web.master.entity.Rout;
import com.qf.openapi.web.master.service.RoutService;
import com.qf.openapi.web.master.vo.RoutRegistVo;
import com.qf.openapi.web.master.vo.TableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;

@Service
public class RoutServiceImpl implements RoutService {
    @Autowired
    RoutDao routDao;
    private String filepath = "E:\\JavaProject\\Idea\\File\\file\\";

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public TableVo<Rout> selectAll(Integer page, Integer limit, String gatewayApiName, String state) {

        int countAll = routDao.CountAll();
        //拦截器在查所有之前
        PageHelper.startPage(page, limit);
        List<Rout> routs = routDao.selectAll(gatewayApiName, state);
        return TableVo.ok(countAll, routs);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addRout(RoutRegistVo routRegistVo) {
        Rout rout = new Rout(null, routRegistVo.getGatewayApiName(), routRegistVo.getInsideApiUrl(), routRegistVo.getServiceId(),
                routRegistVo.getDescription(), routRegistVo.getState(), routRegistVo.getIdempotents(), routRegistVo.getNeedfee());
        int index = routDao.addRout(rout);
        if (index != 0) {
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateRout(Rout rout) {
        int index = routDao.updateRout(rout);
        if (index != 0) {
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteRout(Integer[] ids) {
        int index = 0;
        for (Integer id : ids) {
            index = routDao.deleteRout(id);
        }

        if (index != 0) {
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String export(String gatewayApiName, String state) {
        List<Rout> routList = routDao.selectAll(gatewayApiName, state);
        String filename = UUID.randomUUID() + ".csv";
        String finalFilepath = filepath + filename;
        CsvWriter writer = CsvUtil.getWriter(finalFilepath, StandardCharsets.UTF_8);
        writer.writeBeans(routList);
        return filename;//文件名返回前端
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void getListUse(MultipartFile file) throws IOException {
        String filename = UUID.randomUUID() + ".csv";
        String finalFilepath = filepath + filename;
        file.transferTo(new File(finalFilepath));
        CsvReader reader = CsvUtil.getReader();
        List<Rout> routList = reader.read(ResourceUtil.getUtf8Reader(finalFilepath), Rout.class);
        routDao.insertListRout(routList);
    }
}
